
using Youz.Common.Entity;
using Youz.Common.Helper;
using Youz.Common.DAL;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

namespace Youz.Common.BLL
{
    /// <summary>
    /// CMS_Article BLL Class
    /// </summary>
    public partial class CMS_ArticleBLL : BaseBLL
    {
		CMS_ArticleDAL dal;
		
        public CMS_ArticleBLL()
        {
			dal = new CMS_ArticleDAL();
			base.ModuleName = "CMS_Article";
            base.MenuName = "CMS_Article";
		}

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(CMS_Article model)
        {
            var r = dal.Add(model);

            base.WriteOperationLog(OperationType.Add, r, Utility.GetJsonByObjectHaveTime(model));

            return r;
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(CMS_Article model)
        {
            var r = dal.Update(model);

            base.WriteOperationLog(OperationType.Update, r, Utility.GetJsonByObjectHaveTime(model));

            return r;
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Int64 ID)
        {
            var r = dal.Delete(ID);
			
			base.WriteOperationLog(OperationType.Delete, r, ID);

            return r;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CMS_Article Get(Int64 ID)
        {
            return dal.Get(ID);
        }
		
		/// <summary>
        /// 得到一个JSON对象
        /// </summary>
        public string GetJson(Int64 ID)
        {
            CMS_Article model = Get(ID);
            model.Content = model.Content.Replace('[', '<').Replace(']', '>');
            return Utility.GetJsonByObjectHaveTime(model);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            return dal.Gets(sqlWhere);
        }
		
		/// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataTable Gets(int top, string sqlWhere, string showColumn, params SqlParameter[] cmdParms)
        {
            return dal.Gets(top, sqlWhere, showColumn, cmdParms);
        }

        public DataTable GetsTop()
        {
            var where = " IsPublish=1 and MenuID=18 ";
            DataTable dt = new CMS_ArticleBLL().Gets(5, where, "ID,Title,PublishTime");
            return dt;
        }
		
		/// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            return dal.GetRecordCount(sqlWhere);
        }
		
		/// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            return dal.Gets(sqlWhere, pageindex, pagesize, out total, cmdParms);
        }

        public List<CMS_Article> Gets(int menuID, string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            sqlWhere = string.Format("MenuID={0}", menuID) + (string.IsNullOrEmpty(sqlWhere) ? "" : (string.Format(" and (Title like '%{0}%' or Content like '%{0}%')", sqlWhere)));

            var ds = Gets(sqlWhere, pageindex, pagesize, out total, cmdParms);

            return new EntityHelper<CMS_Article>().FillEntity(ds);
        }
        #endregion  成员方法

        /// <summary>
        /// 获取热门三个文章
        /// </summary>
        /// <param name="menuID"></param>
        /// <returns></returns>
        public List<CMS_Article> GetHots(int menuID)
        {
            var ds = dal.GetHots(menuID);

            return new EntityHelper<CMS_Article>().FillEntity(ds);
        }

        /// <summary>
        /// 获取最新三个文章
        /// </summary>
        /// <param name="menuID"></param>
        /// <returns></returns>
        public List<CMS_Article> GetNews(int menuID)
        {
            var ds = dal.GetNews(menuID);

            return new EntityHelper<CMS_Article>().FillEntity(ds);
        }

        /// <summary>
        /// 获得文章的上下文章
        /// </summary>
        /// <param name="menuID"></param>
        /// <returns></returns>
        public Dictionary<int, CMS_Article> GetNearby(long id)
        {
            var ds = dal.GetNearby(id);
            var prev = CommonHelper.GetDataRow(ds.Tables[0]);
            var next = CommonHelper.GetDataRow(ds.Tables[1]);

            var dict = new Dictionary<int, CMS_Article>();
            dict.Add(1, new EntityHelper<CMS_Article>().FillEntity(prev));
            dict.Add(2, new EntityHelper<CMS_Article>().FillEntity(next));
            return dict;
        }

        /// <summary>
        /// 获得未提交到百度的链接
        /// </summary>
        /// <returns></returns>
        public string GetUnsubmitUrl()
        {
            var dt = CommonHelper.GetDateTable(dal.GetUnsubmitUrl());
            StringBuilder sb = new StringBuilder();

            foreach (DataRow row in dt.Rows)
            {
                if (row["MenuID"].ToString() == "19")
                {
                    sb.AppendFormat("http://www.youzsoft.com/Project/{0}", row["ID"]);
                }
                else
                {
                    sb.AppendFormat("http://www.youzsoft.com/News/{0}", row["ID"]);
                } 
                sb.AppendLine();
            }

            return sb.ToString();
        }
    }
}


  